//图像渲染
//测试链接：https://leetcode.cn/problems/flood-fill/
public class FloodFill {
    int[] dx = {-1,1,0,0};
    int[] dy = {0,0,-1,1};
    int n,m;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        if(image[sr][sc] == color) return image;

        n = image.length; m = image[0].length;
        int temp = image[sr][sc];
        image[sr][sc] = color;
        dfs(image,sr,sc,color,temp);

        return image;
    }

    public void dfs(int[][] image, int i, int j, int color, int temp) {
        for(int k = 0; k < 4; k++) {
            int x = dx[k] + i;
            int y = dy[k] + j;
            if(x >= 0 && x < n && y >= 0 && y < m && image[x][y] == temp ) {
                image[x][y] = color;
                dfs(image, x, y, color,temp);
            }
        }
    }
}
